package test;

import jm.JMC;
import jm.music.data.Part;
import jm.music.data.Phrase;
import jm.music.data.Score;
import jm.util.Play;
import jm.util.View;
import jm.util.Write;

/**
 * Generates phrases and places them in a random order within parts.
 * @author Andrew Brown
 */
public final class Arrange implements JMC{
	public static void main(String[] args){
		//Create the data objects we want to use
		Score score = new Score("An Arrangement");
		//Parts can have a name, channel, and instrument.
		Part melodyPart = new Part("Melodies", CLARINET, 0);
		
		//Lets write the phrases in a convenient way.
	 	int[] pitchArray = {c4,c4,c4,d4,e4,e4,d4,e4,f4,g4,c5,c5,c5,g4,
                                    g4,g4,e4,e4,e4,c4,c4,c4,g4,f4,e4,d4,c4};
		double[] rhythmArray = {C,C,.67,.33,C,.67,.33,.67,.33,M,.33,.33,
                                        .34,.33,.33,.34,.33,.33,.34,.33,.33,.34,
                                        .67,.33,.67,.33,M};
							
		int[] birthdayPitches = {c4, c4, d4, c4, f4, e4, c4, c4, d4, c4,
                                         g4, f4, c4, c4, c5, a4, f4, e4, d4, 
                                         bf4, bf4, a4, f4, g4, f4}; 
		double[] birthdayRhythm = {0.67, 0.33, 1.0, 1.0, 1.0, 2.0, 0.67, 
                                           0.33, 1.0,1.0, 1.0, 2.0, 0.67, 0.33, 
                                           1.0, 1.0, 1.0, 1.0, 1.0, 0.67, 0.33, 
                                           1.0, 1.0, 1.0, 2.0};
		int[] minuetPitches = {d4, f4, bf4, e4, f4, a4, bf3, d4, g4, 
                                       cs4, d4, f4, g3, g3, f3, g3, a3, f4, e4, 
                                       d4, e4, a4, b3, c4, a3};
		double[] minuetRhythm = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
                                         0.5, 0.5, 0.5, 0.5, 1.0, 1.5, 0.125, 
                                         0.125, 3.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
                                         1.0, 2.0, 3.0};
								
		//create Row your boat phrase
		Phrase phrase1 = new Phrase();
		phrase1.addNoteList(pitchArray, rhythmArray);
		
		//create happy birthday phrase
		Phrase phrase2 = new Phrase();
		phrase2.addNoteList(birthdayPitches, birthdayRhythm);
		
		//create the minuet phrase
		Phrase phrase3 = new Phrase();
		phrase3.addNoteList(minuetPitches, minuetRhythm);
		
		//Add the phrases to an array
		Phrase[] allPhrases = {phrase1, phrase2, phrase3};
		
		//randomly arranage the phrases into a part
		int r;
		double addTime =0.0;
		for(int i=0; i<20;i++) {
			//select a random phrase
			 r = (int)(Math.random() * allPhrases.length);
			//print to the screen which one was chosen
			System.out.println("Phrase "+i+" is  phrase number "+ 
                                                                            r);
			// add a copy of the chosen phrase to the part
			melodyPart.addPhrase(allPhrases[r].copy());
			
                        // display extra information
                        System.out.println("Phrase length is "+ 
                                           allPhrases[r].getEndTime());
			System.out.println("Part end time is "+
                                           melodyPart.getEndTime() +
                                           " acumulated time = " + 
                                           (addTime += 
                                            allPhrases[r].getEndTime()));
		}

		//add part to the score
		score.addPart(melodyPart);		
		score.setTempo (130.0);
		
		//OK now write to a MIDI file
//		Write.midi(score, "Arrangement.mid");
		Play.midi(score);
		
		//display
		View.show(score);
	}
}
